<?php
include('connection.php');
 include('simplehtmldom_1_5/simple_html_dom.php');
// get DOM from URL or file

$selEventQuery="select * from subscriber where advocate_status='Enabled'";
	$resEvent=mysql_query($selEventQuery);
	$rowCountEvent=mysql_num_rows($resEvent);
	if($rowCountEvent!=0){
		while($arrViewNews=mysql_fetch_array($resEvent))
		{  	
			$name_list[]= $arrViewNews['advocate_name'];
			$subscriber_ids[]=$arrViewNews['subscriber_id'];
		}
	}
							
$folder=false; 
 for($i=0;$i<sizeof($name_list);$i++)
 {

$html = file_get_html('http://localhost/causelist/index.php?act='.$name_list[$i]);
$ret = $html->find('pre');
$url="http://causelists.nic.in/cgi-bin/chennai_new/omon/lawquery.cgi";
$advocate_no=$subscriber_ids[$i];
$ret[0]=str_replace("<pre>","",$ret[0]);
$ret[0]=str_replace("</pre>","",$ret[0]);

if($folder==false)
{
$cDate=date("Y-m-d");
$workingPath=getcwd();


$structure = $workingPath."\\".$cDate;

if (!mkdir($structure, 0, true)) {
    //die('Failed to create folders...');
}
	$folder=true;
}
$myFile = $cDate."\\".$advocate_no."_".$name_list[$i];

$fh = fopen($myFile, 'a') or die("can't open file");	
$stringData = $ret[0];
fwrite($fh, $stringData);
fclose($fh);

 $ins="insert into cause_pretext (pretext_url,pretext_search_term,pretext_subscriber_id,pretext_content,pretext_date,pretext_timestamp,pretext_status) values ('".$url."','','".$advocate_no."','".addslashes($ret[0])."',".date('Y-m-d').",'','Enabled')";


$res=mysql_query($ins);
}
?>